package ar.com.tallerdos.smartdinner.dao;

import java.util.List;

import ar.com.tallerdos.smartdinner.data.EntitiesDataManager;
import ar.com.tallerdos.smartdinner.model.Identity;


public abstract class BaseDao<T extends Identity> {

    @SuppressWarnings("unchecked")
    public T findById(Integer id) {
	return (T) EntitiesDataManager.getInstance().getEntityById(getClazz(), id);
    }
    
    @SuppressWarnings("unchecked")
    public List<T> findAll() {
	return (List<T>) EntitiesDataManager.getInstance().getAllEntities(getClazz());
    }
    
    public void create(T entity) {
	EntitiesDataManager.getInstance().putEntity(entity);	
    }
    
    public Boolean delete(T entity) {
	return EntitiesDataManager.getInstance().deleteEntity(entity);
    }
    
    public Boolean delete(Integer id) {
	return EntitiesDataManager.getInstance().deleteEntity(getClazz(), id);
    }
    
    public Boolean update(T entity) {
	return EntitiesDataManager.getInstance().updateEntity(entity);
    }
    
    public Boolean createOrUpdate(T entity) {
	if (!this.update(entity)) {
	    this.create(entity);
	    return true;
	}
	return false;
    }
    
    protected abstract Class<T> getClazz();
    
}
